package com.ruoyi.common.utils;

import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;

import java.io.IOException;

/**
 * user-agent解析浏览器相关数据
 * @author chengjian 
 * @date 2021/10/20
 */
public class UserAgentUtil {
    static UASparser uasParser = null;

    // 初始化uasParser对象
    static {
        try {
            uasParser = new UASparser(OnlineUpdater.getVendoredInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static UserAgentInfo getUserAgentInfo(String userAgent) {
        try{
            return UserAgentUtil.uasParser.parse(userAgent);
        } catch (IOException exception){
            exception.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {
        String str = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36 QQBrowser/4.5.123.400";
        String str2 = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36";
        System.out.println(str2);
        try {
            UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(str);
            System.out.println("操作系统名称：" + userAgentInfo.getOsFamily());
            System.out.println("操作系统：" + userAgentInfo.getOsName());
            System.out.println("浏览器名称：" + userAgentInfo.getUaFamily());
            System.out.println("浏览器版本：" + userAgentInfo.getBrowserVersionInfo());
            System.out.println("设备类型：" + userAgentInfo.getDeviceType());
            System.out.println("浏览器:" + userAgentInfo.getUaName());
            System.out.println("类型：" + userAgentInfo.getType());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
